package com.ischoolbar.programmer.controller.admin;

import com.github.pagehelper.util.StringUtil;
import com.ischoolbar.programmer.dao.admin.RoleDao;
import com.ischoolbar.programmer.entity.admin.Authority;
import com.ischoolbar.programmer.entity.admin.Menu;
import com.ischoolbar.programmer.entity.admin.Role;
import com.ischoolbar.programmer.page.admin.Page;
import com.ischoolbar.programmer.service.admin.IAuthorityService;
import com.ischoolbar.programmer.service.admin.IMenuService;
import com.ischoolbar.programmer.service.admin.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    IRoleService roleService;

    @Autowired
    IMenuService menuService;

    @Autowired
    IAuthorityService authorityService;

    /*
    * 返回用户列表
    * */
    @RequestMapping(value="/list", method= RequestMethod.GET)
    public String role_list_page() {
        return "role/list";
    }

    @RequestMapping(value="/list", method= RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> role_list_data(Page page,
                                              @RequestParam(value = "name", required = false, defaultValue = "") String name,
                                              Model model) {
        Map<String, Object> queryMap = new HashMap<>();
        Map<String, Object> ret = new HashMap<>();

        queryMap.put("name", name);
        queryMap.put("pageSize", page.getRows());
        queryMap.put("offset", page.getOffset());
        List<Role> roleList = roleService.findRoleList(queryMap);

        ret.put("rows", roleList);
        ret.put("total", roleService.totalCount());

        return ret;
    }

    /*
    * 添加用户
    * */
    @RequestMapping(value="/add_role", method=RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> add_role(Role role) {
        Map<String, Object> ret = new HashMap<>();
        if (role == null) {
            ret.put("type", "error");
            ret.put("msg", "请填写正确的用户信息");
            return ret;
        }

        if (StringUtil.isEmpty(role.getName())) {
            ret.put("type", "error");
            ret.put("msg", "用户名不能为空");
            return ret;
        }

//        if (role.getId() == null) {
//            role.setId(0);
//        }

        roleService.add(role);

        ret.put("type", "success");
        ret.put("msg", "添加成功");
        return ret;
    }

    /*
    * 删除方法
    * */
    @RequestMapping("/delete_role")
    @ResponseBody
    public Map<String, Object> delete(@RequestParam(value = "id", required = true) Integer id) {
        Map<String, Object> ret = new HashMap<>();
        if (id == null) {
            ret.put("type", "error");
            ret.put("msg", "未获取到前端传入的id");
            return ret;
        }

        try {
            roleService.deleteRole(id);
            ret.put("type", "success");
            ret.put("msg", "删除成功");
        }catch(Exception e) {
            ret.put("type", "error");
            ret.put("msg", "该角色存在权限或者用户信息, 不能删除!!!");
        }



        return ret;
    }

    /*
    * 修改角色信息
    * */
    @RequestMapping("/edit_role")
    @ResponseBody
    public Map<String, Object> edit(Role role) {
        Map<String, Object> ret = new HashMap<>();
        if (role == null) {
            ret.put("type", "error");
            ret.put("msg", "请填写正确的角色信息");
            return ret;
        }

        if (StringUtil.isEmpty(role.getName())) {
            ret.put("type", "error");
            ret.put("msg", "角色名称不能为空");
            return ret;
        }

        roleService.edit(role);
        ret.put("type", "success");
        ret.put("msg", "修改成功");
        return ret;
    }


    /*
    * 查找所有菜单列表并返回给前台
    * */
    @RequestMapping(value="/get_all_menu_list", method=RequestMethod.POST)
    @ResponseBody
    public List<Menu> findAllMenuList() {
        Map<String, Object> queryMap = new HashMap<>();
        Map<String, Object> ret = new HashMap<>();

        queryMap.put("offset", 0);
        queryMap.put("pageSize", 9999);
        List<Menu> list = menuService.findList(queryMap);

//        ret.put("menu_list", list);
        return list;
    }

    /*
    * 添加角色权限
    * */
    @RequestMapping(value = "/add_authority", method=RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> add_authority(@RequestParam(value="ids", required = true) String ids,
            @RequestParam(value="roleId", required = true) String roleId
    ) {
        Map<String, Object> ret = new HashMap<>();
        if (StringUtil.isEmpty(ids)) {
            ret.put("type", "error");
            ret.put("msg", "未选择添加的权限");
            return ret;
        }

        if (StringUtil.isEmpty(roleId)) {
            ret.put("type", "error");
            ret.put("msg", "为接收到被修改角色的id值");
            return ret;
        }

        String[] idsArr = ids.split(",");
        if (idsArr.length > 0) {
            authorityService.delete(Integer.valueOf(roleId));
        }

        for (String id : idsArr) {
            Authority authority = new Authority();
            authority.setMenuId(Integer.valueOf(id));
            authority.setRoleId(Integer.valueOf(roleId));

            authorityService.add(authority);
        }

        /*
        * 先进行删除, 在进行添加
        * */
        ret.put("type", "success");
        ret.put("msg", "编辑成功");


        return ret;
    }

    /*
    * 根据角色id查找角色列表
    * */
    @ResponseBody
    @RequestMapping(value = "/get_authority_list", method = RequestMethod.POST)
    public List<Authority> getAuthorityList(
            @RequestParam(value = "roleId", required = true) Integer roleId
    ) {
        return authorityService.findAuthorityByRoleId(roleId);

    }
}
